# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=qtkeychain
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6")
pkgver=0.15.0
pkgrel=1
pkgdesc='Platform-independent Qt API for storing passwords securely. (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('spdx:BSD-3-Clause')
url="https://github.com/frankosterfeld/qtkeychain"
depends=("${MINGW_PACKAGE_PREFIX}-libsecret")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-clang-libs"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")
source=("https://github.com/frankosterfeld/qtkeychain/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        0001-qtkeychain-cmake-remove-utf8.patch
        0002-qtkeychain-include-cmath.patch)
sha256sums=('f4254dc8f0933b06d90672d683eab08ef770acd8336e44dfa030ce041dc2ca22'
            '0742745489f28d1a806b616e716462ba3f17c910bc3e627a8aaebb3538466d9b'
            '7b22914e8fe0ccc3fea3be66fbf1081c2aff78311704198b75a1c3fd56a13a50')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  _apply_patch_with_msg \
    0001-qtkeychain-cmake-remove-utf8.patch \
    0002-qtkeychain-include-cmath.patch
}

build() {
  # qtkeychain-qt5
  mkdir "${srcdir}"/build-${MSYSTEM}-qt5 && cd "${srcdir}"/build-${MSYSTEM}-qt5

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_WITH_QT6=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  # qtkeychain-qt6
  mkdir "${srcdir}"/build-${MSYSTEM}-qt6 && cd "${srcdir}"/build-${MSYSTEM}-qt6

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_WITH_QT6=ON \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package_qtkeychain-qt5() {
  pkgdesc='Platform-independent Qt API for storing passwords securely - Qt5. (mingw-w64)'
  depends+=("${MINGW_PACKAGE_PREFIX}-qt5-base")
  cd "${srcdir}/build-${MSYSTEM}-qt5"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}

package_qtkeychain-qt6() {
  pkgdesc='Platform-independent Qt API for storing passwords securely - Qt6. (mingw-w64)'
  depends+=("${MINGW_PACKAGE_PREFIX}-qt6-base")
  cd "${srcdir}/build-${MSYSTEM}-qt6"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
